package com.book.lizhi.database;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.book.lizhi.model.ArticleModel;
import com.book.lizhi.model.BookshelfModel;
import com.book.lizhi.model.ChapterModel;
import com.book.lizhi.model.DetailBookModel;

public class DataBaseMannager {
	private static DataBaseMannager instance = new DataBaseMannager();
	private AppDatabaseHelper mDatabaseHelper = null;

	public static DataBaseMannager getInstance() {
		return instance;
	}

	public AppDatabaseHelper getDatabaseHelper(Context context) {
		if (mDatabaseHelper == null) {
			mDatabaseHelper = new AppDatabaseHelper(context, AppDatabase.DATABASE_NAME, null, 4);
		}
		return mDatabaseHelper;
	}

	public void deleteValueById(Context context, String id) {
		SQLiteDatabase database = getDatabaseHelper(context).getWritableDatabase();
		database.delete(AppDatabase.BOOK_TABLE_NAME, "id=?", new String[] { id });
		database.close();
	}

	public void deleteFavById(Context context, String id) {
		SQLiteDatabase database = getDatabaseHelper(context).getWritableDatabase();
		database.delete(AppDatabase.FAV_TABLE_NAME, "id=?", new String[] { id });
		database.close();
	}

	public void insertFave(Context context, ArticleModel articleModel) {

		SQLiteDatabase database = getDatabaseHelper(context).getWritableDatabase();
		ContentValues values = new ContentValues();

		JSONObject object = (JSONObject) JSON.toJSON(articleModel);
		Set<String> keySet = object.keySet();
		for (String property : keySet) {
			values.put(property, object.getString(property));
		}

		database.insert(AppDatabase.FAV_TABLE_NAME, "id", values);
		database.close();

	}

	public void insertValue(Context context, DetailBookModel bookshelfModel) {

		SQLiteDatabase database = getDatabaseHelper(context).getWritableDatabase();
		ContentValues values = new ContentValues();
		JSONObject object = (JSONObject) JSON.toJSON(bookshelfModel);
		Set<String> keySet = object.keySet();
		for (String property : keySet) {
			values.put(property, object.getString(property));
		}
		database.insertOrThrow(AppDatabase.BOOK_TABLE_NAME, "id", values);
		database.close();

	}

	public void updateReadTime(Context context, String id) {

		SQLiteDatabase database = getDatabaseHelper(context).getWritableDatabase();
		Cursor cursor = database.query(AppDatabase.BOOK_TABLE_NAME, null, "id=?", new String[] { id }, null, null, null);
		int index = cursor.getColumnIndex("_id");
		int bookIndex = 0;
		for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
			bookIndex = cursor.getInt(index);
		}

		bookIndex++;
		ContentValues values = new ContentValues();
		values.put("_id", bookIndex);
		database.update(AppDatabase.BOOK_TABLE_NAME, values, "id=?", new String[] { id });
		database.close();
	}

	public void updateValue(Context context, String id, BookShelfInfo info, String value) {
		SQLiteDatabase database = getDatabaseHelper(context).getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(info.getMsg(), value);
		database.update(AppDatabase.BOOK_TABLE_NAME, values, "id=?", new String[] { id });
		database.close();
	}

	public boolean isFav(Context context, String id) {
		boolean isFav = false;
		SQLiteDatabase database = getDatabaseHelper(context).getWritableDatabase();
		Cursor cursor = database.query(AppDatabase.FAV_TABLE_NAME, null, "id=?", new String[] { id }, null, null, null);
		if (cursor.getCount() > 0) {
			isFav = true;
		}
		database.close();
		return isFav;
	}

	public List<ArticleModel> queryFavs(Context context) {
		ArrayList<ArticleModel> list = new ArrayList<ArticleModel>();
		SQLiteDatabase database = getDatabaseHelper(context).getWritableDatabase();
		Cursor cursor = database.query(AppDatabase.FAV_TABLE_NAME, null, null, null, null, null, "_id desc");

		if (cursor == null) {
			return list;
		}

		for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
			ArticleModel articleModel = new ArticleModel();
			JSONObject object = (JSONObject) JSON.toJSON(articleModel);
			Set<String> keySet = object.keySet();
			for (String property : keySet) {
				String value = cursor.getString(cursor.getColumnIndex(property));
				object.put(property, value);
			}

			list.add(JSON.toJavaObject(object, ArticleModel.class));
		}
		cursor.close();
		database.close();
		return list;
	}

	public List<BookshelfModel> queryBookshelfs(Context context) {
		ArrayList<BookshelfModel> list = new ArrayList<BookshelfModel>();
		SQLiteDatabase database = getDatabaseHelper(context).getWritableDatabase();
		Cursor cursor = database.query(AppDatabase.BOOK_TABLE_NAME, null, null, null, null, null, "_id desc");

		if (cursor == null) {
			return list;
		}

		for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
			List<ChapterModel> chapterModels = null;
			BookshelfModel bookshelfModel = new BookshelfModel();
			JSONObject object = (JSONObject) JSON.toJSON(bookshelfModel);
			Set<String> keySet = object.keySet();
			for (String property : keySet) {
				String value = cursor.getString(cursor.getColumnIndex(property));
				if ("chapterModels".equals(property)) {
					chapterModels = JSON.parseArray(value, ChapterModel.class);
					continue;
				}
				object.put(property, value);
			}

			BookshelfModel tempBookshelfModel = JSON.parseObject(object.toJSONString(), BookshelfModel.class);
			tempBookshelfModel.chapterModels = chapterModels;
			list.add(tempBookshelfModel);
		}
		cursor.close();
		database.close();
		return list;
	}
}
